package com.lxm.ymcc.mq;



import com.alibaba.fastjson.JSON;
import com.lxm.ymcc.constents.MqConstants;
import com.lxm.ymcc.dto.StanionMqDto;
import com.lxm.ymcc.service.IMessageStationService;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.spring.annotation.MessageModel;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
@RocketMQMessageListener(topic = MqConstants.STANTIC_TOPIC,
        selectorExpression=MqConstants.STANTIC_TAGES	//tags
        ,consumerGroup = "service-course-consumer"
        ,messageModel = MessageModel.CLUSTERING ) //消息的消费模式 CLUSTERING：集群    BROADCASTING：广播
@Slf4j
public class CourseMesage implements RocketMQListener<MessageExt> {

    @Autowired
    private IMessageStationService messageStationService;
    @Override
    public void onMessage(MessageExt messageExt) {
        //1.获取原始消息
        byte[] body = messageExt.getBody();
        //2.转换为字符串
        String s = new String(body);
        //3.转换为StationInMQDto
        StanionMqDto stationInMQDto = JSON.parseObject(s, StanionMqDto.class);
        //4.保存数据到数据库
        messageStationService.save(stationInMQDto);
    }
}
